package com.ruoyi.project.channel.channelAccountGroup.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.channelAccountGroup.domain.ChannelAccountGroup;
import com.ruoyi.project.channel.channelAccountGroup.service.IChannelAccountGroupService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 账号分组信息操作处理
 * 
 * @author CAIN
 * @date 2019-09-17
 */
@Controller
@RequestMapping("/channel/channelAccountGroup")
public class ChannelAccountGroupController extends BaseController
{
    private String prefix = "channel/channelAccountGroup";
	
	@Autowired
	private IChannelAccountGroupService channelAccountGroupService;

	@RequiresPermissions("channel:channelAccountGroup:view")
	@GetMapping()
	public String channelAccountGroup()
	{
	    return prefix + "/channelAccountGroup";
	}
	
	/**
	 * 查询账号分组列表
	 */
	@RequiresPermissions("channel:channelAccountGroup:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ChannelAccountGroup channelAccountGroup)
	{
		startPage();
		User user = getSysUser();
		String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
		if (roleKey.equals("channel")) {
			channelAccountGroup.setChannelId(user.getUserId());
		}

        List<ChannelAccountGroup> list = channelAccountGroupService.selectChannelAccountGroupList(channelAccountGroup);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出账号分组列表
	 */
	@RequiresPermissions("channel:channelAccountGroup:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChannelAccountGroup channelAccountGroup)
    {
		User user = getSysUser();
		String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
		if (roleKey.equals("channel")) {
			channelAccountGroup.setChannelId(user.getUserId());
		}
    	List<ChannelAccountGroup> list = channelAccountGroupService.selectChannelAccountGroupList(channelAccountGroup);
        ExcelUtil<ChannelAccountGroup> util = new ExcelUtil<ChannelAccountGroup>(ChannelAccountGroup.class);
        return util.exportExcel(list, "channelAccountGroup");
    }
	
	/**
	 * 新增账号分组
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存账号分组
	 */
	@RequiresPermissions("channel:channelAccountGroup:add")
	@Log(title = "账号分组", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ChannelAccountGroup channelAccountGroup)
	{
		User user = getSysUser();
		String roleKey = ShiroUtils.getSysUser().getRoles().get(0).getRoleKey();
		if (roleKey.equals("channel")) {
			channelAccountGroup.setChannelId(user.getUserId());
		}
		return toAjax(channelAccountGroupService.insertChannelAccountGroup(channelAccountGroup));
	}

	/**
	 * 修改账号分组
	 */
	@GetMapping("/edit/{groupId}")
	public String edit(@PathVariable("groupId") Long groupId, ModelMap mmap)
	{
		ChannelAccountGroup channelAccountGroup = channelAccountGroupService.selectChannelAccountGroupById(groupId);
		mmap.put("channelAccountGroup", channelAccountGroup);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存账号分组
	 */
	@RequiresPermissions("channel:channelAccountGroup:edit")
	@Log(title = "账号分组", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ChannelAccountGroup channelAccountGroup)
	{		
		return toAjax(channelAccountGroupService.updateChannelAccountGroup(channelAccountGroup));
	}
	
	/**
	 * 删除账号分组
	 */
	@RequiresPermissions("channel:channelAccountGroup:remove")
	@Log(title = "账号分组", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(channelAccountGroupService.deleteChannelAccountGroupByIds(ids));
	}
	
}
